<template>
	<el-row :gutter="20" style="padding:20px 10px;margin:0px">
		<el-form ref="form" label-width="80px" size="mini" label-position="left">
			<el-col :span="12" class="mt">
				<el-form-item label="项目名称">
					<el-input :value="sinfo.name" disabled></el-input>
				</el-form-item>

				<el-form-item label="项目编号">
					<el-input :value="sinfo.number" disabled></el-input>
				</el-form-item>

				<el-form-item label="项目价格">
					<el-input v-model="sinfo.price"></el-input>
				</el-form-item>

				<el-form-item label="项目用时">
					<el-input v-model="sinfo.usetime"></el-input>
				</el-form-item>
				<el-form-item>
				    <el-button type="primary" @click="submit">保存</el-button>
				    <el-button @click="close">取消</el-button>
				  </el-form-item>
				
			</el-col>
			<el-col :span="12" class="mt">
				<el-form-item label="销售提成">
					<el-radio-group v-model="sinfo.xstdtype">
						<el-radio :label="1">百分比</el-radio>
						<el-radio :label="2">固定金额</el-radio>
					</el-radio-group>
					<el-input v-model="sinfo.xspercent" placeholder="请输入项目销售百分比提成(0 ~ 100)" v-if="sinfo.xstdtype==1"></el-input>
					<el-input v-model="sinfo.xsfixed" placeholder="请输入项目销售固定金额提成" v-else></el-input>
				</el-form-item>

				<el-form-item label="手工提成">
					<el-radio-group v-model="sinfo.sgtdtype">
						<el-radio :label="1">百分比</el-radio>
						<el-radio :label="2">固定金额</el-radio>
					</el-radio-group>
					<el-input v-model="sinfo.sgpercent" placeholder="请输入项目手工百分比提成(0 ~ 100)" v-if="sinfo.sgtdtype==1"></el-input>
					<el-input v-model="sinfo.sgfixed" placeholder="请输入项目手工固定金额提成" v-else></el-input>
				</el-form-item>

				<el-form-item label="卡耗提成">
					<el-radio-group v-model="sinfo.khtdtype">
						<el-radio :label="1">百分比</el-radio>
						<el-radio :label="2">固定金额</el-radio>
					</el-radio-group>
					<el-input v-model="sinfo.khpercent" placeholder="请输入项目卡耗百分比提成(0 ~ 100)" v-if="sinfo.khtdtype==1"></el-input>
					<el-input v-model="sinfo.khfixed" placeholder="请输入项目卡耗固定金额提成" v-else></el-input>
				</el-form-item>

			</el-col>
		</el-form>
	</el-row>
</template>
<script>
import C from '#/src/common/common.js'
import Subject from '#/src/shop/subject.js'
export default {
    name: 'editSubject',
    data () {
        return {
            sinfo: {}
        }
    },
    methods: {
        async submit () {
            if (!/^[1-9]\d*(\.\d+)?$/.test(this.sinfo.price)) {
                C.error('请正确输入项目价格')
                return false
            } else if (!/^[1-9]\d*$/.test(this.sinfo.usetime)) {
                C.error('请输入项目完成时间')
                return false
            }

            if (this.sinfo.xstdtype==1) {
                this.sinfo.xsfixed = 0
                if (!/^(\d|[1-9]\d|100)$/.test(this.sinfo.xspercent)) {
                    C.error('请输入销售提成比(0 ~ 100)')
                    return false
                }
            } else {
                this.sinfo.xspercent = 0
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.sinfo.xsfixed)) {
                    C.error('请输入销售提成金额')
                    return false
                }
            }

            if (this.sinfo.sgtdtype==1) {
                this.sinfo.sgfixed = 0
                if (!/^(\d|[1-9]\d|100)$/.test(this.sinfo.sgpercent)) {
                    C.error('请输入手工提成比(0 ~ 100)')
                    return false
                }
            } else {
                this.sinfo.sgpercent = 0
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.sinfo.sgfixed)) {
                    C.error('请输入手工提成金额')
                    return false
                }
            }

            if (this.sinfo.khtdtype==1) {
                this.sinfo.khfixed = 0
                if (!/^(\d|[1-9]\d|100)$/.test(this.sinfo.khpercent)) {
                    C.error('请输入卡耗提成比(0 ~ 100)')
                    return false
                }
            } else {
                this.sinfo.khpercent = 0
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.sinfo.khfixed)) {
                    C.error('请输入卡耗提成金额')
                    return false
                }
            }
            var xssum = 0
            var sgsum = 0
            var khsum = 0
            if(this.sinfo.xstdtype === 1){
                xssum = parseInt(this.sinfo.price)*parseInt(this.sinfo.xspercent)/100
            }else{
                xssum = parseInt(this.sinfo.xsfixed)
            }

            if(this.sinfo.sgtdtype === 1){
                sgsum = parseInt(this.sinfo.price)*parseInt(this.sinfo.sgpercent)/100
            }else{
                sgsum = parseInt(this.sinfo.sgfixed)
            }

            if(this.sinfo.khtdtype === 1){
                khsum = parseInt(this.sinfo.price)*parseInt(this.sinfo.khpercent)/100
            }else{
                khsum = parseInt(this.sinfo.khfixed)
            }
            var allsum = parseInt(xssum+sgsum+khsum)
            if(this.sinfo.price < allsum){
                C.error('输入的提成超出项目价格')
                return false
            }
            let res = await Subject.editSubject(this.sinfo)
            C.closeBrowserWindow(res)
        },
        close () {
            C.closeBrowserWindow()
        }
    },
    mounted: function(){
        let row = C.getDatas()
        row.xspercent = Number(row.xspercent*100).toFixed(0)
        row.sgpercent = Number(row.sgpercent*100).toFixed(0)
        row.khpercent = Number(row.khpercent*100).toFixed(0)
        this.sinfo = row
    }
}
</script>